/*
 * StrongT - Sistema Web
 * Copyright 2011-2012
 * 
 * Autor               : Enrique Morocco Tupa
 * Fecha Registro      : 04/12/2012 08:10:31 PM
 * Detalle             : AppReport
 */

package com.strategy.modulo.web.util;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;

/**
 *
 * @author Enrique Morocco Tupa
 */
public class AppReport extends HttpServlet {

    private List rptData;
    private String rptNombre;
    private Map rptParametro;


    public void dataSetReport(String tNombre, Map tParametro, List tData) {
        this.rptNombre = tNombre;
        this.rptData = tData;
        this.rptParametro = tParametro;
    }

    public void generarReportePdf() {
        //La Ruta de nuestro reporte
        System.out.print(":::R-A:::");
        try {
            System.out.print(":::NOMBRE REPORT :::" + this.rptNombre);
            JasperReport reporte = (JasperReport) JRLoader.loadObject(getServletContext().getRealPath("jasper/" + this.rptNombre + ".jasper"));
            System.out.print(":::R-B:::");
            Map mapParams = new HashMap();
            mapParams.put("PAR_LOGO_INSTITUCIONAL", AppParams.URL_APPICATION + "\\img\\report\\logoinstitucional.png");
            JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, mapParams, new JRBeanCollectionDataSource(this.rptData));
            System.out.print(":::R-C:::");
            JasperExportManager.exportReportToPdfFile(jasperPrint,
                    "C:\\_eliminar\\" + this.rptNombre + ".pdf");
            
            File path = new File("C:\\_eliminar\\" + this.rptNombre + ".pdf");
            Desktop.getDesktop().open(path);
            System.out.print(":::R-D:::");

        } catch (Exception e) {
            System.out.print("ERROR IDENTIFICADO :::" + e.toString());
        }
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/pdf");
        ServletOutputStream out = response.getOutputStream();
        List listaPariticipantes = new ArrayList();
        for (int i = 1; i <= 10; i++) {
            /*RptDatosPersonaEvento p = new RptDatosPersonaEvento();
            p.setNombreCompleto("Prueba");
            listaPariticipantes.add(p);*/
        }
        try {
            System.out.print(":::processRequest REPORT :::" + this.rptNombre);
            JasperReport reporte = (JasperReport) JRLoader.loadObject(getServletContext().getRealPath("jasper/rptCongregacionEventosPrincipal.jasper"));
            Map parametros = new HashMap();
            parametros.put("autor", "Juan");
            parametros.put("titulo", "Reporte");
            JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, parametros, new JRBeanCollectionDataSource(listaPariticipantes));
            System.out.print(":::A:::");
            JRExporter exporter = new JRPdfExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
            System.out.print(":::B:::");
            exporter.exportReport();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo()
    {
        return "Short description";
    }

    public List getRptData() {
        return rptData;
    }

    public void setRptData(List rptData) {
        this.rptData = rptData;
    }

    public String getRptNombre() {
        return rptNombre;
    }

    public void setRptNombre(String rptNombre) {
        this.rptNombre = rptNombre;
    }

    public Map getRptParametro() {
        return rptParametro;
    }

    public void setRptParametro(Map rptParametro) {
        this.rptParametro = rptParametro;
    }
}
